package com.example.baiducrabs.screenrecorderdemo;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.projection.MediaProjection;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button mstartBt;
    private Button mstopBt;
    private Button synthesisBtBt;
    private static final int REQUEST_CODE = 1;
    private Activity mActivity;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();
        verifyStoragePermissions(this);

    }

    private void initListener() {
        mstartBt.setOnClickListener(this);
        mstopBt.setOnClickListener(this);
        synthesisBtBt.setOnClickListener(this);

    }

    private void initView() {
        mActivity=this;
        mstartBt = (Button) findViewById(R.id.startBt);
        mstopBt = (Button)findViewById(R.id.stopBt);
        synthesisBtBt = (Button) findViewById(R.id.SynthesisBt);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.startBt:
                Intent captureIntent = ScreenRecorderUtils.getInstance(mActivity).mMediaProjectionManager.createScreenCaptureIntent();
                startActivityForResult(captureIntent, REQUEST_CODE);
                break;
            case R.id.stopBt:
                ScreenRecorderUtils.getInstance(mActivity).stopRecording();
                break;
            case R.id.SynthesisBt:
                ScreenRecorderUtils.getInstance(mActivity).SynthesisRecording();
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            ScreenRecorderUtils.getInstance(mActivity).startRecording(resultCode,data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ScreenRecorderUtils.getInstance(mActivity).onClose();
    }


    // Storage Permissions
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
            Manifest.permission.RECORD_AUDIO};

    /**
     * * Checks if the app has permission to write to device storage
     * *
     * * If the app does not has permission then the user will be prompted to
     * * grant permissions
     * *
     * * @param activity
     */
    public static void verifyStoragePermissions(Activity activity) {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE);
        }
    }
}
